package cumeo89.chii.client.util;

import java.util.ArrayList;
import java.util.List;


public abstract class AbstractAction implements Action {

	private List<MyChangeListener> listeners = new ArrayList<MyChangeListener>();
	private boolean enabled = true;

	public final void execute() {
		if (enabled) {
			executeImpl();
		}
	}

	protected abstract void executeImpl();

	protected void fireChangeEvent() {
		for (MyChangeListener l : listeners) {
			l.onChanged(this);
		}
	}

	public void addChangeListener(MyChangeListener l) {
		if (!listeners.contains(l)) {
			listeners.add(l);
		}
	}

	public void removeChangeListener(MyChangeListener l) {
		listeners.remove(l);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean b) {
		if (this.enabled != b) {
			this.enabled = b;
			fireChangeEvent();
		}
	}

}
